/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q0167.java
 * @Author : WangRuoyu
 * @Date 2022/12/16 22:04
 */

package leetcode;

import java.util.Arrays;

public class Q0167 {
    //    public int[] twoSum(int[] numbers, int target) {
//        int[] res = new int[2];
//
//        int i = 0;
//        int j = numbers.length - 1;
//
//        while (i < j) {
//            int tmp = numbers[i] + numbers[j];
//            if (tmp == target) {
//                res[0] = i + 1;
//                res[1] = j + 1;
//                break;
//            } else if (tmp > target) {
//                j -= 1;
//            } else {
//                i += 1;
//            }
//        }
//        return res;
//    }
    public int[] twoSum(int[] numbers, int target) {
        int i = 0;
        int j = numbers.length - 1;
        while (i < j) {
            if (numbers[i] + numbers[j] == target) {
                break;
            } else if (numbers[i] + numbers[j] > target) j--;
            else i++;
        }
        return new int[]{i + 1, j + 1};
    }

    public static void main(String[] args) {
        Q0167 sl = new Q0167();
        int[] numbers = {2, 7, 11, 15};
        int target = 9;
        System.out.println(Arrays.toString(sl.twoSum(numbers, target)));
    }
}
